﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BI.CF.BLL;
using BI.CF.Model;
using BI.CF.PageList;
using System.Runtime.Serialization.Json;


namespace BI.CF.Company.Controllers
{
    public class MemLoanController : Controller
    {
        //
        // GET: /MemLoan/
        #region  重写MemLoanBLL
        MemLoanBLL bll = new MemLoanBLL();
        #endregion
        #region 成功案例显示
        /// <summary>
        /// 成功案例显示
        /// </summary>
        /// <returns></returns>
        public JsonResult MemLoan_Show()
        {

            int pageindex = int.Parse(Request["pageindex"].ToString());
            int pageSize = int.Parse(Request["pageSize"].ToString());
            string RepaymentModeId = Request["RepaymentModeId"].ToString();
            if (RepaymentModeId == "0")
            {
                RepaymentModeId = "";
            }
            else if (RepaymentModeId == "1")
            {
                RepaymentModeId = "按月分期";
            }
            else if (RepaymentModeId == "2")
            {
                RepaymentModeId = "按月到期";
            }
            else if (RepaymentModeId == "3")
            {
                RepaymentModeId = "按天到期";
            }
            string ExamineState = Request["ExamineState"].ToString();
            if (ExamineState == "0")
            {
                ExamineState = "";
            }
            Pager<MemLoanModel> li = bll.Get_MemLoan_Selet(RepaymentModeId, ExamineState, pageindex, pageSize);

            return Json(li);

        }
        #endregion
        #region 显示某一具体借款信息
        /// <summary>
        /// 显示某一具体借款信息
        /// </summary>
        /// <returns></returns>
        public ActionResult Show_One()
        {
            if (Request["id"] == null)
            {
                return Content("<script>location.href='/Home/Index'</script>");

            }
            else
            {
                Session["ID"] = Request["id"].ToString();
                return View();
            }
        }
        #endregion
        #region 查询具体
        /// <summary>
        /// 查询具体
        /// </summary>
        /// <returns></returns>
        public JsonResult Show_About()
        {
            string ID = Request["id"].ToString();
            MemLoanModel m = bll.Get_MemLoanOne(ID);
            return Json(m);
        }
        #endregion
        #region  重写MemInvest_BLL
        MemInvest_BLL membll = new MemInvest_BLL();
        #endregion
        #region 对某一标的投资信息
        /// <summary>
        /// 对某一标的投资信息
        /// </summary>
        /// <returns></returns>
        public JsonResult MemInvest_Only()
        {
            string id = Request["MemLoanId"].ToString();
            List<MemInvestModel> li = membll.Get_MemInvest(id);
            return Json(li);
        }
        #endregion
        #region 借款类型选择页面
        /// <summary>
        /// 借款类型选择页面
        /// </summary>
        /// <returns></returns>
        public ActionResult MemLoanType()
        {
            return View();
        }
        #endregion
        #region 发布借款验证交易密码
        /// <summary>
        /// 发布借款验证交易密码
        /// </summary>
        /// <returns></returns>
        public ActionResult MemLoan()
        {
            return View();
        }
        #endregion
        #region 判断是否是会员
        /// <summary>
        /// 判断是否是会员
        /// </summary>
        /// <returns></returns>
        public JsonResult UserrsSelect()
        {
            if (Session["Umodel"] != null)
            {
                UserrsModel model = Session["Umodel"] as UserrsModel;
                return Json(model);
            }
            else
            {
                return Json(0);
            }
        }
        #endregion
        #region 查询当前登录的交易密码
        /// <summary>
        /// 查询当前登录的交易密码
        /// </summary>
        /// <returns></returns>
        public int SelectPayPwd()
        {
            UserrsModel model = Session["Umodel"] as UserrsModel;
            UserrsModel PayPwd = bll.SelectPayPwd(model.UserId);
            return PayPwd.PaymentPwd;
        }
        #endregion
        #region 修改支付密码的页面
        /// <summary>
        /// 修改支付密码的页面
        /// </summary>
        /// <returns></returns>
        public ActionResult UpdatePwd()
        {
            return View();
        }
        #endregion
        #region 修改支付密码
        /// <summary>
        /// 修改支付密码
        /// </summary>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public int UpdatePwdDo(string pwd)
        {
            UserrsModel model = Session["Umodel"] as UserrsModel;
            if (bll.UpdatePwd(model.UserId, pwd) == true)
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }
        #endregion
        #region 添加发布借款详细信息视图
        /// <summary>
        /// 添加发布借款详细信息视图
        /// </summary>
        /// <returns></returns>
        public ActionResult MemLoanView()
        {
            if (Session["Umodel"] == null)
            {
                return Content("<script>location.href='/MyAccount/View_UserLogin'</script>");
            }
            else
            {
                return View();
            }
        }
        #endregion
        #region 发布借款信息
        /// <summary>
        /// 发布借款信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ActionResult MemLoanAdd(MemLoanModel model)
        {
            UserrsModel UsModel = Session["Umodel"] as UserrsModel;
            model.UserrId = UsModel.UserId;
            model.LaunchLoanTime =DateTime.Now;  
            model.EndLoanTime = model.LaunchLoanTime.AddDays(model.LoanRaise);
            model.PayInterest =Convert.ToDecimal(model.Annualrate / 100 * float.Parse(model.LoanMoney.ToString())/12 * model.LoanTerm);
            model.RepaySumMoney = model.LoanMoney + model.PayInterest;
            model.RepaymentDay = DateTime.Now;
            model.ExamineState = "0";
            model.AlreadyRaiseLoan = 0;
            model.TotalTransactions = 0;
            if (bll.MemLoanAdd(model)>0)
            {
                return Content("<script>alert('借款发布中');location.href=''</script>");
            }
            else
            {
                return Content("<script>alert('发布失败')</script>");
            }
        }
        #endregion
        #region 实例化MemIncome_BLL
        MemIncome_BLL Come_B = new MemIncome_BLL();
        #endregion
        #region 抽象某位会员投资收益信息
        /// <summary>
        /// 抽象某位会员投资收益信息
        /// </summary>
        /// <returns></returns>
        public JsonResult MemIncon_Only()
        {
            int ID = int.Parse(Request["ID"].ToString());
            MemIncomeModel m = Come_B.Get_MemIncome(ID);
            return Json(m);
        }
        #endregion
        #region 实例化Autonym_BLL
        Autonym_BLL ab = new Autonym_BLL();
        #endregion
        #region 根据借款查询用户实名
        /// <summary>
        /// 根据借款查询用户实名
        /// </summary>
        /// <returns></returns>
        public JsonResult Authonym_Only()
        {
            int ID = int.Parse(Request["ID"].ToString());
            AutonymModel a = ab.Get_Authonym_Only(ID);
            return Json(a);
        }
        #endregion
        #region 根据借款编号查询用户详细信息
        /// <summary>
        /// 根据借款编号查询用户详细信息
        /// </summary>
        /// <returns></returns>
        public JsonResult User_Only()
        {
            UserBLL b = new UserBLL();
            int ID = int.Parse(Request["ID"].ToString());
            UserrsModel m = b.Get_OnlyUsers(ID);
            return Json(m);
        }
        #endregion
        #region 查询用户积分 信息
        /// <summary>
        /// 查询用户积分 信息
        /// </summary>
        /// <returns></returns>
        public JsonResult Integral_Only()
        {
            UserBLL b = new UserBLL();
            int ID = int.Parse(Request["ID"].ToString());
            IntegralModel m = b.Get_OnlyIntegral(ID);
            return Json(m);
        }
        #endregion
        #region 成功案例展示(more)

        /// <summary>
        /// 成功案例展示(more)
        /// </summary>
        /// <returns></returns>
        public ActionResult More_MemLoan()
        {
            return View();
        }
        #endregion
        #region 添加正在招标的借款视图
        /// <summary>
        /// 添加正在招标的借款视图
        /// </summary>
        /// <returns></returns>
        public ActionResult MemloanIng()
        {
            return View();
        }
        #endregion
        #region 添加成功的借款视图
        /// <summanry>
        /// 添加成功的借款视图
        /// </summary>
        /// <returns></returns>
        public ActionResult MemloanFinish()
        {
            return View();
        }
        #endregion
        #region 查询借款信息
        /// <summary>
        /// 查询借款信息
        /// </summary>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <param name="Title"></param>
        /// <param name="MinTime"></param>
        /// <param name="MaxTime"></param>
        /// <param name="State"></param>
        /// <param name="Userid"></param>
        /// <returns></returns>
        public JsonResult MemloanIngDo(int pageSize, int pageIndex, string Title, string MinTime, string MaxTime, string State, int Userid)
        {
            #region 查询借款信息
            Pager<MemLoanModel> Plist = bll.SelectMomLoanMone(pageSize,pageIndex, Title, MinTime, MaxTime, State, Userid);
            return Json(Plist);
            #endregion
        }
        #endregion
        #region 查询待还记录
        /// <summary>
        /// 查询待还记录
        /// </summary>
        /// <returns></returns>
        public JsonResult Rep_about()
        {
            #region 查询待还记录
            int id = int.Parse(Request["ID"].ToString());
            List<RepayDetailedModel> li = bll.GetOne_Rep(id);
            return Json(li);
            #endregion
        }
        #endregion
    }
}
